import {
    CompleteState,
    AnswerState,
    Cardinality,
    BaseType
} from './ServiceConst'



let CardinalityChecker = {
    /**
     * 比对单个答案是否正确
     * @param baseType 答案项类型
     * @param correctValue 正确答案
     * @param currentValue 用户答案
     * @returns {boolean}
     */
    checkOneValue(baseType, correctValue, currentValue) {
        //如果两个都是object直接进行全等判断
        if (typeof correctValue === 'object' && typeof currentValue === 'object') {
            correctValue = JSON.stringify(correctValue);
            currentValue = JSON.stringify(currentValue);
            return correctValue === currentValue;
        }

        switch (baseType) {
            case BaseType.STRING:
            case BaseType.BOOLEAN:
            case BaseType.FLOAT:
            case BaseType.INTEGER:
                return correctValue === currentValue;
            case BaseType.ARRAY:
            case BaseType.PAIR:
            case BaseType.JSON:
                return JSON.stringify(correctValue) === JSON.stringify(currentValue);
            default:
                //没有指定类型，则直接进行全等判断
                if (typeof correctValue === 'object' && typeof currentValue === 'object') {
                    correctValue = JSON.stringify(correctValue);
                    currentValue = JSON.stringify(currentValue);
                }
                return correctValue === currentValue;
        }
    },
    /**
     * 单个答案判断
     * 
     * @param {any} baseType 
     * @param {any} correctAnswerVal 
     * @param {any} userAnswerVal 
     * @returns 
     */
    single(baseType, correctAnswerVal, userAnswerVal) {
        return this.checkOneValue(baseType, correctAnswerVal[0], userAnswerVal[0]);
    },
    /**
     * 答案顺序必须匹配
     * 
     * @param {any} baseType 
     * @param {any} correctAnswerVal 
     * @param {any} userAnswerVal 
     * @returns 
     */
    ordered(baseType, correctAnswerVal, userAnswerVal) {
        let i,
            result,
            correctLength = correctAnswerVal.length

        //长度必须相等
        if (correctLength !== userAnswerVal.length) {
            return false;
        }

        //顺序必须一致
        for (i = 0; i < correctLength; i++) {
            result = this.checkOneValue(baseType, correctAnswerVal[i], userAnswerVal[i]);
            //只要有一个不匹配，答案错误
            if (result === false) {
                break;
            }
        }
        return result;
    },
    /**
     * 答案顺序可不匹配
     * 
     * @param {any} baseType 
     * @param {any} correctAnswerVal 
     * @param {any} userAnswerVal 
     * @returns 
     */
    multiple(baseType, correctAnswerVal, userAnswerVal) {
        let i,
            result,
            correctLength = correctAnswerVal.length,
            correctItem,
            tempAnswer = userAnswerVal.slice();


        //长度必须相等
        if (correctLength !== userAnswerVal.length) {
            return false;
        }

        //顺序可不一致
        for (i = 0; i < correctLength; i++) {
            correctItem = correctAnswerVal[i];
            var whileCount = 0;
            var answerLength = tempAnswer.length;
            while (tempAnswer.length) {
                let temp = tempAnswer.shift();
                result = this.checkOneValue(baseType, correctItem, temp);
                if (result) {
                    break;
                } else {
                    tempAnswer.push(temp);
                }
                whileCount++;
                if (whileCount >= answerLength) {
                    result = false;
                    break;
                }
            }
            if (result === false) {
                break;
            }
        }
        return result;
    },
    getCompleteState: function (answeredCount, correctAnswerLength) {

        let unAnsweredCount = correctAnswerLength - answeredCount;
        //正确答案可能为空
        if (unAnsweredCount < 0) {
            unAnsweredCount = 0;
        }

        if (answeredCount === 0) {
            return CompleteState.UNANSWERED;
        } else if (answeredCount === correctAnswerLength) {
            return CompleteState.ALL_ANSWERED;
        } else {
            return CompleteState.PARTICAL_ANSWERED;
        }

    },
    checkAnswer: function (correctAnswer, userAnswerVal) {
        let correctVal = correctAnswer.value || [],
            correctLength = correctVal.length,
            cardinality = correctAnswer.cardinality,
            baseType = correctAnswer.baseType,
            result;

        if (!correctAnswer || !correctLength) {
            //没有正确答案，默认认为做对
            result = true;
        } else if (userAnswerVal.length !== correctLength) {
            //用户答案的长度和正确答案的长度不一致，答题错误
            result = false;
        } else {
            result = this[cardinality](baseType, correctVal, userAnswerVal);
        }
        return result;
    }

}

export default CardinalityChecker